package cn.wangdazhuang;

import com.msino.dict.StarDictReader;
import com.msino.dict.manager.DictsManager;
import com.msino.dict.service.IDictsManager;
import com.msino.dict.service.core.Word;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Test case of class <code>StarDictSearcher</code>
 * @author XuDong.Su
 * @version 1.0,  Apr 5, 2009
 * @see com.msino.dict.StarDictSearcher
 */
public class StarDictSearcher {
    private StarDictReader instance;
    private IDictsManager dictsManager = new DictsManager();

    private String dictsProp = dictsManager.getDictProps();

    String dictFileDir = "";
    String dictName = "";

    //查询结果
    Word result = null;

    /**
     * Default constructor.
     */
    public StarDictSearcher() {

    	dictFileDir = dictsProp.substring(0, dictsProp.indexOf("||"));
    	dictName = dictsProp.substring(dictsProp.indexOf("||") + 2, dictsProp.length());

    	System.out.println("dictFileDir : " + dictFileDir);
	System.out.println("dictName : " + dictName);


        try {
            instance =  new StarDictReader(dictFileDir, dictName);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StarDictSearcher.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Test of lookup method, of class DictEngine.
     * @throws java.lang.InterruptedException Thread.sleep(long)
     */
    public String lookup(String word) {
        System.out.println("lookup");
        try {
            long start = System.currentTimeMillis();
            System.out.println("Time: " + (start) + "ms");

            result = instance.lookup(word);

            System.out.println(result.self + ": " + result.definition);

            long end1 = System.currentTimeMillis();
            System.out.println("Time: " + (end1) + "ms");
            System.out.println("Space Time: " + (end1 - start) + "ms");

        } catch (Exception ex) {
        	System.out.println(ex.getMessage());
                return "没有结果";
            //Logger.getLogger(StarDictSearcher.class.getName()).log(Level.INFO, ex.getMessage());
        }
        return result.definition;
    }

    public static void main(String[] args) throws InterruptedException {
    	StarDictSearcher test = new StarDictSearcher();
    	test.lookup("test");
    }
}